﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace EasyTools.UI
{
    public partial class FormWinHook : Form
    {

        public static int GWL_STYLE = -16;
        public static int WS_CHILD = 0x40000000; //child window
        public static int WS_BORDER = 0x00800000; //window with border
        public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
        public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar
        public static int GWL_EXSTYLE = -20;
        public static int WS_EX_WINDOWEDGE = 0x00000100;
        public static int WS_EX_DLGMODALFRAME = 0x00000001;
        [System.Runtime.InteropServices.DllImport("USER32.DLL")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        [System.Runtime.InteropServices.DllImport("USER32.DLL")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("User32.dll", EntryPoint = "SetParent")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
        public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); //打开窗体方法，fileName是的窗体名称，包含路径
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        List<Process> processes;

        public FormWinHook()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var pros = Process.GetProcesses();
            processes = new List<Process>();
            foreach (Process pro in pros)
            {
                if (pro.ProcessName.Contains(textBox1.Text))
                {
                    processes.Add(pro);

                    //SetParent(pro.MainWindowHandle, this.Handle);
                    //int style = GetWindowLong(pro.MainWindowHandle, GWL_STYLE);
                    //SetWindowLong(pro.MainWindowHandle, GWL_STYLE, (style & ~WS_CAPTION));
                    //SetWindowLong(pro.MainWindowHandle, GWL_EXSTYLE,
                    //GetWindowLong(pro.MainWindowHandle, GWL_EXSTYLE) & ~(WS_EX_WINDOWEDGE | WS_EX_DLGMODALFRAME));
                    
                    //SendMessage(pro.MainWindowHandle，)
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Process pro in processes)
            {
                
                SetParent(pro.MainWindowHandle, this.Handle);
                int style = GetWindowLong(pro.MainWindowHandle, GWL_STYLE);
                SetWindowLong(pro.MainWindowHandle, GWL_STYLE, (style & ~WS_CAPTION));
                SetWindowLong(pro.MainWindowHandle, GWL_EXSTYLE,
                GetWindowLong(pro.MainWindowHandle, GWL_EXSTYLE) & ~(WS_EX_WINDOWEDGE | WS_EX_DLGMODALFRAME));
                MoveWindow(pro.MainWindowHandle, Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text), true);
                ShowWindow(pro.MainWindowHandle,(int)ProcessWindowStyle.Maximized);
                    //SendMessage(pro.MainWindowHandle，)
            }
            
        }

        private void FormWinHook_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (processes == null)
            {
                return;
            }
            foreach (Process pro in processes)
            {
                try
                {
                    pro.Kill();
                }
                catch (Exception ex) { }
            }
        }
    }
}
